package bb.lanxing.util;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.widget.Toast;

import com.bes.bessdk.scan.BtHeleper;
import com.umeng.socialize.net.dplus.CommonNetImpl;
import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.lib.devices.utils.BleUtils;
import bb.lanxing.view.BiciAlertDialogBuilder;

public class SensorEnableUtil {
    public static boolean isSdkVersionAboveM() {
        return Build.VERSION.SDK_INT >= 23;
    }

    public static boolean isSdkVersionAboveS() {
        return Build.VERSION.SDK_INT >= 31;
    }

    public static boolean isSdkVersionAboveR() {
        return Build.VERSION.SDK_INT >= 30;
    }

     public static boolean checkGpsEnabled(Activity mActivity) {
        LocationManager locationManager = (LocationManager)App.getContext().getSystemService(Context.LOCATION_SERVICE);
        if (locationManager == null)
            return false;
        try {
            boolean bool;
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
                    || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                bool = true;
            } else {
                bool = false;
            }
            if (!bool) {
                Toast.makeText((Context)mActivity, R.string.sport_toast_open_gps_first, Toast.LENGTH_LONG).show();
                turnGPSOn(mActivity);
            }
            return bool;
        } catch (SecurityException securityException) {
            Toast.makeText((Context)mActivity, R.string.sport_toast_gps_permission_fail, Toast.LENGTH_LONG).show();
            return false;
        }
    }

    private static void turnGPSOn(Activity activity) {
        try {
            activity.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean checkGPSPermission(final Activity activity) {
        if (activity == null || RxPermissionUtil.getInstance().checkPermission(activity, RxPermissionUtil.getLocationOnlyPermission())) {
            return true;
        }
        new BiciAlertDialogBuilder(activity).setTitle(R.string.dialog_prompt)
                .setMessage(R.string.sport_gps_permission_not_granted_for_ble)
                .setPositiveButton(R.string.dialog_btn_goto_setting, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                                Uri.fromParts("package", App.getContext().getPackageName(), null));
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        activity.startActivity(intent);
                    }
                })
                .setNegativeButton(R.string.dialog_btn_cancel, (DialogInterface.OnClickListener) null)
                .show();
        return false;
    }

    public static boolean checkBluetooth(Activity activity) {
        App context = App.getContext();
        if (RxPermissionUtil.getInstance().checkPermission(context, RxPermissionUtil.getBluetoothPermissions())) {
            BluetoothAdapter defaultAdapter = BtHeleper.getBluetoothManager(activity).getAdapter();
            if (defaultAdapter == null) {
                Toast.makeText(activity, (int) R.string.device_bluetooth_not_supported, Toast.LENGTH_LONG).show();
                return false;
            } else if (defaultAdapter.isEnabled()) {
                return true;
            } else {
                requestEnableBluetooth(activity);
                return false;
            }
        }
        Toast.makeText(activity, (int) R.string.device_no_permissions, Toast.LENGTH_LONG).show();
        return false;
    }

    public static void requestEnableBluetooth(Activity activity) {
        activity.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 76);
    }

    public static boolean checkBluetoothEnable(Activity activity, int i) {
        if (!BleUtils.isBluetoothEnable(activity)) {
            activity.startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), i);
            return false;
        }
        return true;
    }
}
